package cn.medemede.beans.vo;


import cn.medemede.dao.DatabaseConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

public class User {
    private String check;
    private String username;
    private String zname;
    private String sex;
    private String borth;
    private String phone;
    private String zposition;
    private String email;
    private String favorite;
    private String password;

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {return password;}

    private int age;

    public String getUsername() {
        return username;
    }

    public String getZname() {
        return zname;
    }

    public String getSex() {
        return sex;
    }


    public String getBorth() {
        return borth;
    }


    public String getPhone() {
        return phone;
    }


    public String getZposition() {
        return zposition;
    }

    public String getEmail() {
        return email;
    }


    public String getFavorite() {
        return favorite;
    }

    public int getAge() {
        return age;
    }

    public String getCheck() {return check;}

    public void setCheck(String check) {this.check = check;}

    public void setUsername(String username) {
        this.username = username;
    }

    public void setZname(String zname) {
        this.zname = zname;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setBorth(String borth) {
        this.borth = borth;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setZposition(String zposition) {
        this.zposition = zposition;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setFavorite(String favorite) {
        this.favorite = favorite;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setIt(String username) throws SQLException {
        this.username=username;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection= DatabaseConnection.getConnection();
        Statement statement = connection.createStatement();
        String sql="select * from users where username='"+username+"'";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            zname=rs.getString("zname");
            sex=rs.getString("sex");
            borth=rs.getString("borth");
            phone=rs.getString("phone");
            zposition=rs.getString("zposition");
            email=rs.getString("email");
            favorite=rs.getString("favorite");
        }
        DatabaseConnection.releaseResources(rs,statement,connection);

        //计算年龄
        Calendar c = Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DATE);
        int u_y=Integer.parseInt(borth.substring(0,4));
        int u_m;
        if ('0' == borth.charAt(5)){
            u_m=(int)borth.charAt(6);
        }else {
            u_m=Integer.parseInt(borth.substring(5,7));
        }
        int u_d;
        if ('0' == borth.charAt(8)){
            u_d=(int)borth.charAt(9);
        }else {
            u_d=Integer.parseInt(borth.substring(8,10));
        }


        if (month>u_m){
            age=year-Integer.parseInt(borth.substring(0,4));
        }else if (day>=u_d){
            age=year-Integer.parseInt(borth.substring(0,4));
        }else {
            age=year-Integer.parseInt(borth.substring(0,4))-1;
        }
    }
}
